from unittest import TestCase
from unittest.mock import MagicMock, patch

from django_sso.middleware import CASMiddleware


class MiddlewareTests(TestCase):
    def setUp(self):
        from django.conf import settings

        if not settings.configured:
            settings.configure(DEBUG=True)  # pragma: no cover
        settings.CAS_ADMIN_PREFIX = None

    def test_init(self):
        mock_middleware_mixin = MagicMock()
        _ = CASMiddleware(mock_middleware_mixin)

    def test_process_request(self):
        mock_middleware_mixin = MagicMock()
        mock_request = MagicMock()
        cas_middleware = CASMiddleware(mock_middleware_mixin)
        cas_middleware.process_request(request=mock_request)

    def test_process_view_login(self):
        mock_middleware_mixin = MagicMock()
        mock_request = MagicMock()
        view_func = MagicMock()
        view_args = []
        view_kwargs = {}
        cas_middleware = CASMiddleware(mock_middleware_mixin)
        mock_auth_views = (view_func, MagicMock())
        with patch(
            "django_sso.middleware.cas_middleware.get_auth_views",
            return_value=mock_auth_views,
        ):
            with patch("django_sso.middleware.cas_middleware.sso_login"):
                cas_middleware.process_view(
                    mock_request,
                    view_func,
                    view_args,
                    view_kwargs,
                )

    def test_process_view_logout(self):
        mock_middleware_mixin = MagicMock()
        mock_request = MagicMock()
        view_func = MagicMock()
        view_args = []
        view_kwargs = {}
        cas_middleware = CASMiddleware(mock_middleware_mixin)
        mock_auth_views = (MagicMock(), view_func)
        with patch(
            "django_sso.middleware.cas_middleware.get_auth_views",
            return_value=mock_auth_views,
        ):
            with patch("django_sso.middleware.cas_middleware.sso_logout"):
                cas_middleware.process_view(
                    mock_request,
                    view_func,
                    view_args,
                    view_kwargs,
                )

    def test_process_view_general(self):
        mock_middleware_mixin = MagicMock()
        mock_request = MagicMock()
        view_func = MagicMock()
        view_args = []
        view_kwargs = {}
        cas_middleware = CASMiddleware(mock_middleware_mixin)
        mock_auth_views = (MagicMock(), MagicMock())
        with patch(
            "django_sso.middleware.cas_middleware.get_auth_views",
            return_value=mock_auth_views,
        ):
            cas_middleware.process_view(
                mock_request,
                view_func,
                view_args,
                view_kwargs,
            )


class MiddlewareTestsAdminPrefix(TestCase):
    def setUp(self):
        from django.conf import settings

        if not settings.configured:
            settings.configure(DEBUG=True)  # pragma: no cover
        settings.CAS_ADMIN_PREFIX = "potato"

    def test_process_view_general(self):
        mock_middleware_mixin = MagicMock()
        mock_request = MagicMock()
        mock_request.path.startswith.return_value = False
        view_func = MagicMock()
        view_args = []
        view_kwargs = {}
        cas_middleware = CASMiddleware(mock_middleware_mixin)
        mock_auth_views = (MagicMock(), MagicMock())
        with patch(
            "django_sso.middleware.cas_middleware.get_auth_views",
            return_value=mock_auth_views,
        ):
            cas_middleware.process_view(
                mock_request,
                view_func,
                view_args,
                view_kwargs,
            )

    def test_process_view_general_is_staff(self):
        mock_middleware_mixin = MagicMock()
        mock_request = MagicMock()
        mock_request.path.startswith.return_value = True
        view_func = MagicMock()
        view_args = []
        view_kwargs = {}
        cas_middleware = CASMiddleware(mock_middleware_mixin)
        mock_auth_views = (MagicMock(), MagicMock())
        with patch(
            "django_sso.middleware.cas_middleware.get_auth_views",
            return_value=mock_auth_views,
        ):
            cas_middleware.process_view(
                mock_request,
                view_func,
                view_args,
                view_kwargs,
            )

    def test_process_view_general_not_staff(self):
        mock_middleware_mixin = MagicMock()
        mock_request = MagicMock()
        mock_request.path.startswith.return_value = True
        mock_request.user.is_staff = False
        view_func = MagicMock()
        view_args = []
        view_kwargs = {}
        cas_middleware = CASMiddleware(mock_middleware_mixin)
        mock_auth_views = (MagicMock(), MagicMock())
        with patch(
            "django_sso.middleware.cas_middleware.get_auth_views",
            return_value=mock_auth_views,
        ):
            cas_middleware.process_view(
                mock_request,
                view_func,
                view_args,
                view_kwargs,
            )

    def test_process_view_general_not_authed(self):
        mock_middleware_mixin = MagicMock()
        mock_request = MagicMock()
        mock_request.path.startswith.return_value = True
        mock_request.user.is_authenticated = False
        view_func = MagicMock()
        view_args = []
        view_kwargs = {}
        cas_middleware = CASMiddleware(mock_middleware_mixin)
        mock_auth_views = (MagicMock(), MagicMock())
        with patch(
            "django_sso.middleware.cas_middleware.get_auth_views",
            return_value=mock_auth_views,
        ):
            with patch(
                "django_sso.middleware.cas_middleware.reverse",
                return_value="/",
            ):
                cas_middleware.process_view(
                    mock_request,
                    view_func,
                    view_args,
                    view_kwargs,
                )
